<template>
    <div class="item" :class="'item-' + element.type">
        <div class="item-title-box handle">
            <div class="item-title">{{ element.title }}</div>
            <!-- 移除模块 -->
            <el-icon v-if="sort" style="cursor: pointer" @click="$emit('delete', element)">
                <CloseBold />
            </el-icon>
            <!-- 显示/收起 -->
            <div class="btn" v-else @click="handleToggle">
                <el-icon v-if="showState">
                    <Minus />
                </el-icon>
                <el-icon v-else>
                    <Plus />
                </el-icon>
            </div>
        </div>
        <div v-if="showState" style="min-height: 55px">
            <component :is="getComponent(element.type)" v-if="showState" :ref="setComponentRef" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import RequestBox from "@/components/Index/RequestBox/index.vue";
import PagePersonalTasksBox from "@/components/Index/PagePersonalTasksBox/index.vue";
import PopularKnowledgeBox from "@/components/Index/Knowledge/PopularKnowledgeBox.vue";
import KnowledgeListBox from "@/components/Index/Knowledge/KnowledgeListBox.vue";
import CountRequestBox from "@/components/Index/CountRequestBox/index.vue";
import AfficheBox from "@/components/Index/AfficheBox/index.vue";
import CountRequestFlowTaskBox from "@/components/Index/CountRequestFlowTaskBox/index.vue";
import KnowledgeStatisticsBox from "@/components/Index/Knowledge/StatisticsBox.vue";
// 组件映射
const componentMap = {
    'findRequest': RequestBox,
    'findPagePersonalTasks': PagePersonalTasksBox,
    'knowledgeStatistics': KnowledgeStatisticsBox,
    'findPopularKnowledge': PopularKnowledgeBox,
    'newKnowledge': KnowledgeListBox,
    'countRequest': CountRequestBox,
    'affiche': AfficheBox,
    'countRequestFlowTask': CountRequestFlowTaskBox
};
const props = defineProps({
    element: { type: Object, required: true },
    sort: { type: Boolean, default: false },
    location: { type: String, required: true }
});

const emit = defineEmits(['delete', 'toggle', 'update:show']);

// 使用本地ref来管理显示状态
const showState = ref(props.element.show);



const getComponent = (type: string) => componentMap[type] || null;

const componentRefs = ref<Record<string, any>>({});

const setComponentRef = (el: any) => {
    if (el) {
        componentRefs.value[props.element.type] = el;
    }
};

const handleToggle = () => {
    showState.value = !showState.value;
    emit('toggle', {
        type: props.element.type,
        show: showState.value,
        location: props.location
    });
};
</script>
<style scoped lang="scss">
.home {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-height: 100%;
}

.header {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 52px;

    .tip_sort_box {
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 332px;
        height: 44px;
        padding: 0 16px;
        margin-left: 15px;
        font-size: 14px;
        color: #ffffff;
        background: var(--el-color-primary);
        border-radius: 50px;

        .sort_finish {
            height: 30px;
            padding: 0 16px;
            margin-left: 15px;
            font-size: 14px;
            line-height: 30px;
            color: var(--el-color-primary);
            cursor: pointer;
            background-color: #ffffff;
            border-radius: 50px;
        }
    }
}

.grid-box {
    display: flex;
    flex: 1;
    flex-direction: row;
    align-items: stretch;
    justify-content: space-between;
    overflow: hidden;

    .left-box,
    .right-box {
        width: calc(50% - 5px);
        overflow: hidden;
    }

    .grid-container {
        width: 100%;
        min-height: 100%;

        .item {
            // min-height: 50px;
            margin-bottom: 10px;
            overflow: hidden;
            background-color: #ffffff;
            border: 1px solid #ffffff;
        }

        .chosen {
            border: 1px solid $primary-color !important;
        }

        .item-title-box {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 10px;
            cursor: move;
            border-bottom: 1px solid #e5e4e9;

            .item-title {
                height: 44px;
                font-size: 15px;
                line-height: 44px;
            }

            .btn {
                cursor: pointer;
            }
        }
    }
}
</style>